package com.gnerv.cms.tag;

import com.gnerv.cms.entity.Catalog;
import com.gnerv.cms.freemarker.FreemarkerComponent;
import com.gnerv.cms.service.IArticleService;
import com.gnerv.cms.service.ICatalogService;
import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Map;

/**
 * @author Gnerv LiGen
 * @description
 * @data 2019/4/3
 */
@FreemarkerComponent("g_catalog")
public class CatalogTagDirective implements TemplateDirectiveModel {

    @Autowired
    ICatalogService catalogService;

    @Autowired
    IArticleService articleService;

    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {
        DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
        String bId = map.get("bId").toString();
        Catalog catalog = catalogService.selectCatalog(bId);
        TemplateModel tm = builder.build().wrap(catalog);
        environment.setVariable("catalog", tm);
        templateDirectiveBody.render(environment.getOut());
    }

}
